import Vue from 'vue'
import VueRouter from 'vue-router'
// 登录组件
import MyLogin from '@/components/Login.vue'
// 主页
import MyHome from '@/components/Home.vue'
// welcome组件
import MyWelcome from '@/components/Welcome.vue'
// 用户列表组件
import MyUsers from '@/components/users/User.vue'
// 权限列表组件
import MyRights from '@/components/rights/Rights.vue'
// 角色列表组件
import MyRoles from '@/components/rights/Roles.vue'
// 商品分类组件
import MyCate from '@/components/goods/Cate.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  // 登录页
  { path: '/login', component: MyLogin },
  // 首页
  {
    path: '/home',
    component: MyHome,
    children: [
      { path: '', component: MyWelcome },
      // 用户列表
      { path: '/users', component: MyUsers },
      // 权限管理-权限列表
      { path: '/rights', component: MyRights },
      { path: '/roles', component: MyRoles },
      { path: '/categories', component: MyCate }
    ]
  }
]

const router = new VueRouter({
  routes
})
// 路由实例对象上挂载全局前置路由守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const token = sessionStorage.getItem('token')
  if (!token) return next('/login')
  next()
})

export default router
